require "Common/define"
require "Logic/Config"
require "Common/functions"
require "Net/TitleHandler"
TitleManager = {};
local this = TitleManager;
function TitleManager.init()
    this.titleShowInfo = {};
    this.titleDecGroup = {};
    this.titlesData = {};
    this.isNewTitle = 0;--是否有新勋章 0无 1有
    for k, v in pairs(G.dataTable["t_medal"]) do
        if this.titleDecGroup[v.f_GroupId] == nil then
            this.titleDecGroup[v.f_GroupId] = {};
        end
        table.insert(this.titleDecGroup[v.f_GroupId], v);
        table.sort(this.titleDecGroup[v.f_GroupId], function(a, b)
            return a.f_LevelId > b.f_LevelId
        end)
        table.insert(this.titlesData, v);
    end
    table.sort(this.titlesData, function(a, b)
        return a.f_Id < b.f_Id
    end)
end

function TitleManager.GetTitleGroupInfo()
    return this.titleDecGroup;
end

--遍历拥有的勋章 若勋章过期则替换为低一级勋章
function TitleManager.SetTitleShowInfo(data)
    local state = false;
    local titlesData = data;
    this.titleShowInfo = data;
    for i = 1, #titlesData.showList do
        if G.dataTable["t_medal"][tostring(titlesData.showList[i])] == nil then
            break ;
        end
        local groupId = G.dataTable["t_medal"][tostring(titlesData.showList[i])].f_GroupId
        local titleInfo = this.GetHaveTitleById(titlesData.showList[i]);
        local isExpired = (titleInfo ~= nil and (titleInfo.lastTime - HelperFunc.GetServerTime()) <= 0 and titleInfo.lastTime ~= -1)
        --if isExpired or titleInfo == nil then
            local group = this.titleDecGroup[groupId]
            for j = 1,#group  do
                local title = this.GetHaveTitleById(group[j].f_Id);
                if title == titleInfo and not isExpired then
                    break
                end
                if title ~= nil and (title.lastTime - HelperFunc.GetServerTime() >= 0 or title.lastTime == -1) then
                    titlesData.showList[i] = title.id;
                    state = true;
                    break
                end
            end
        --end
    end
    this.titleShowInfo = titlesData;
    if state then
        TitleHandler:SaveShowTitle(this.titleShowInfo.showList);
    end
    return state;
end

function TitleManager.SetShowList(list)
    this.titleShowInfo = list;
end

function TitleManager.GetTitleData()
    return this.titleShowInfo;
end

function TitleManager.GetHaveTitleById(id)
    local titleList = this.titleShowInfo.titleList;
    for i = 1, #titleList do
        if titleList[i].id == id then
            return titleList[i];
        end
    end
    return nil;
end;

--获取新的勋章之后 若等级高则替换掉等级低的
function TitleManager.GetTitleRefresh(msg)
    local id = msg.title.id;
    local msgData = G.dataTable["t_medal"][tostring(id)]
    local groupId = msgData.f_GroupId;
    isNewTitle = 1;
    if msg.style == 2 then
        --暂时先不管，徽章到期后，在延时算不算激活不清楚
        --有效时间延长
        --UISysTips.AddMsg(string.format(getLanguage("Sys_MsgMedalTimeLimitExtend"), getLanguage(msgData.f_MedalName)))
    --else
        UISysTips.AddMsg(string.format(getLanguage("Sys_MsgMedalAcquired"), getLanguage(msgData.f_MedalName)))
        isNewTitle = 0;
    end
    for i = 1, #this.titleShowInfo.showList do
        local data = G.dataTable["t_medal"][(tostring(this.titleShowInfo.showList[i]))];
        local checkGroupId = data.f_GroupId;
        if checkGroupId == groupId and msgData.f_LevelId > data.f_LevelId then
            this.titleShowInfo.showList[i] = id;
            TitleHandler:SaveShowTitle(this.titleShowInfo.showList);
            return true;
        end
    end
    return false;
end

function TitleManager.setIsNewTitle(is)
    isNewTitle = is;
end

function TitleManager.getIsNewTitle()
    if FuncOpenManager.isOpen(Config.FuncOpenType.Title) then
        return isNewTitle;
    end
    return 0;
end

--获取激活徽章的总属性加成
function TitleManager.GetAllAttr()
    local allActiveBadgesInfo = {}    --已经激活的徽章
    for k,v in pairs(this.GetTitleGroupInfo()) do
        for m,n in pairs(v) do
            local titleInfo = this.GetHaveTitleById(n.f_Id);
            if titleInfo ~= nil and ((titleInfo.lastTime - HelperFunc.GetServerTime()) > 0 or titleInfo.lastTime == -1) then
                table.insert(allActiveBadgesInfo,titleInfo)
                break ;
            end
        end
    end
    local allAttr = {}
    if #allActiveBadgesInfo > 0 then
        for k,v in pairs(allActiveBadgesInfo) do
            local oneAttr = decodeJsonStrToTabel(G.dataTable["t_medal"][tostring(v.id)].f_CharacterValue)
            for m,n in pairs(oneAttr) do
                if allAttr[n[1]] then
                    allAttr[n[1]] = allAttr[n[1]]+n[2]
                else
                    allAttr[n[1]] = {}
                    allAttr[n[1]] = n[2]
                end
            end
        end
    end
    return allAttr
end

return TitleManager